/**
 * Created by user on 2017/3/17.
 */
module.exports = function (caseBarAccountService,$cookies,$scope,paginationService,$rootScope) {

  var vm = this;
//获取案件类型
  $scope.type=$cookies.get('type')


  //当前用户案件的id
  vm.caseId = $cookies.get('caseAppId');
  //取出当前用户名字
  vm.userPortraitName=$cookies.get('userPortraitName');
  //获取当前用户的头像
  vm.userPortraitSave=$cookies.get('userPortraitSave');
  vm.pageSize = 10;
  vm.accountIncome = accountIncome;
  // vm.accountExpend = accountExpend;
  vm.accountEditIncome = accountEditIncome;
  vm.accountEditExpend = accountEditExpend;
  vm.closeBtn = closeBtn;
  vm.caseBarAccDel = caseBarAccDel;
  vm.caseBarSureDel = caseBarSureDel;
  vm.accountListSearch=accountListSearch;
  vm.accountType=accountType;
  vm.accountDelete=accountDelete;
  vm.accountEdit=accountEdit;
  vm.accountAdd=accountAdd;
  vm.accountSave=accountSave;
  vm.clearFilter=clearFilter;
  vm.accountBlurIf=accountBlurIf;
  vm.accountBlurI=accountBlurI;
  vm.accountSubmitIf=accountSubmitIf;
  vm.accountFocusIf=accountFocusIf;
  vm.accountFocusI=accountFocusI;
  // vm.changePage=changePage;
  vm.pageSetting3 = paginationService.getDefaultPageSetting();
  //记账状态判断
  vm.caseAccountName = caseAccountName;
  //筛选传进的参数
  vm.accountFilterParams={
    caseId:vm.caseId,
    pageNum:1,
    pageSize:vm.pageSize,
    name : "",
    startDate:'',
    endDate:'',
    flow:''
  };
  $rootScope.typeIf!=7?$('.L_accRight').removeClass('marginRight'):$('.L_accRight').addClass('marginRight');

  //每个表单的配置，如果不设置，默认和全局配置相同
  vm.validateOptions = {
    showError: false
  };

  //返回的筛选参数
  vm.filterBackParams={};
  //添加传进的参数
  vm.accountAddParams={caseId:vm.caseId};
  //类型的返回
  vm.typeList={};
  vm.account={};
  vm.Earning=0;
  vm.Expend=0;
  vm.accountAddParams = {
    // accountDate: new Date()
  };
  //删除记账的id
  vm.deleteId = '';

  //收入
  vm.AccIncome = {
    showPopup:false,
    showEditIncome:false,
  };
  //支出
  vm.AccExpend = {
    showPopup:false,
    showEditExpend:false,
  };
  //记账收入icon
  vm.showIcon = {
    Icons:false,//显示Icon小图标
    showColors:"#333",
    hideColors:"#d2d2d2",
  };
  //删除
  vm.showDel = {
    delText:false,//
  };
  //清除
  vm.clearCon = {
    showText:false,
  };
  //类型的颜色
  vm.typeListColor = {
    showColor:'#000',
    hideColor:'#d2d2d2',
  };
  vm.accountAddParams.accountDate = ""
  //打开记账收入
  function accountIncome(type) {
    $('input[name^="amount"]').css('border', '1px solid #d9d9d9');
    $('input[name^="content"]').css('border', '1px solid #d9d9d9');
    vm.showIcon = '';
    vm.accountAddParams={
      content:"",
      amount: ''
    };
    if(type==1){
      $('#showADD').show();
      $('#showRe').hide();
    }else{
      $('#showADD').hide();
      $('#showRe').show();
    }
    vm.AccIncome.showPopup = true;
    vm.accountAddParams.accountDate = getNowFormatDate();
    // 清空参数
    vm.showIcon = '';
    vm.accountAddParams = { };
    vm.accountAddParams.accountDate=getNowFormatDate()
  }
  //打开记账支出
  // function accountExpend() {
  //   vm.AccExpend.showPopup = true;
  //   vm.accountAddParams.accountDate = new Date();
  // }


  //打开删除
  function caseBarAccDel() {
    vm.showDel.delText = true;
  }
  //删除
  function caseBarSureDel(account) {
    vm.account = angular.copy(account);
    vm.deleteId = vm.account.id;
    //打开删除
    caseBarAccDel();
    // vm.showDel.delText = false;

  }
  //关闭弹窗 (记账收入、支出、删除)
  function closeBtn() {
    vm.AccExpend.showPopup = false;
    vm.AccIncome.showPopup = false;
    vm.showDel.delText = false;
    vm.AccIncome.showEditIncome = false;
    vm.AccExpend.showEditExpend = false;
    $('#editAccountIncomMyModal').modal('hide');
    $('#addAccountIncomeMyModal').modal('hide');

    vm.showIcon = '';
    vm.accountAddParams = { };
    vm.accountAddParams.accountDate=getNowFormatDate();
  }
  //打开记账收入的编辑页面
  function accountEditIncome(id) {
    vm.showIcon=id;
    vm.AccIncome.showEditIncome = true;
  }
  //打开记账支出的编辑页面
  function accountEditExpend(id) {
    vm.showIcon=id;
    vm.AccExpend.showEditExpend = true;
  }
  //清除筛选
  function clearFilter() {
    vm.accountFilterParams.name = null;
    vm.accountFilterParams.startDate = null;
    vm.accountFilterParams.endDate = null;
    vm.clearCon.showText = false;
    vm.accountFilterParams.flow = '';
    //刷新页面
    accountListSearch(vm.caseId,1,10);
    accountType();
    init();
  }
  vm.validateOptions = {
    blurTrig: false,
    showError: false,
  };

  //初始化
  function init() {
    accountListSearch(vm.caseId,1,10);
    accountType();
  }
  init();

  /**
   * 日期选择器
   */
  $(".accountDate").datetimepicker({
    minView: "month",
    format: 'yyyy-mm-dd',
    language: "zh-CN",
    charset: "utf-8",
  }).on('changeDate', function (ev) {
    $('.accountDate').datetimepicker('hide');
  });


  /**
   * 更改日期事件
   */
  $('.form_datetime')
    .datetimepicker({
      minView: "month",
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        charset: "utf-8",
    })
    .on('changeDate', function (ev) {
      $('.form_datetime').datetimepicker('hide');
      var d = new Date(ev.date);
      var a = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();

      var b = new Date(a).pattern("yyyy-MM-dd");

    });

  $scope.accountFilterParams = vm.accountFilterParams;
  var watch = $scope.$watch('accountFilterParams',function(newValue,oldValue, scope){
    accountListSearch(vm.caseId,1,10);
  }, true);

  //记账类型
  function accountType() {
    caseBarAccountService.accountType().then(function (response) {
      vm.typeList = response.data.data;
    });
  }

  //筛选
  function accountListSearch(caseId,pageNum,pageSize) {
    var params = {
      'caseId':vm.caseId,
      'startDate':vm.accountFilterParams.startDate,
      'endDate':vm.accountFilterParams.endDate,
      'name':vm.accountFilterParams.name,
      'flow':vm.accountFilterParams.flow,
      'begin': (vm.pageSetting3.pageNo- 1)*vm.pageSetting3.pageSize,
      'pageSize': vm.pageSetting3.pageSize};
    caseBarAccountService.accountListSearch(params).then(function (response) {
      if(response.data.code==1){
        vm.filterBackParams = response.data.data;
        vm.Earning=vm.Expend=0;
        angular.forEach(vm.filterBackParams,function (item, index) {
          if (item.moneyflow === 1) {
            vm.Earning += item.amount;
          } else if (item.moneyflow === 2) {
            vm.Expend += item.amount;
          }
        });
        vm.pageSetting3.totalItems = response.data.totalCount;
        if(vm.filterBackParams == [] || vm.filterBackParams == '' || vm.filterBackParams == null){
          $('.L_caseBarAccList').css('display','none');
        }else if(vm.filterBackParams != []){
          $('.L_caseBarAccList').css('display','block');
        }

      }
    })
  }
  //删除记账记录
  function accountDelete() {
    //发送请求(带参数)
    var params = {
      "id":vm.deleteId,
      "caseId":vm.caseId
    };
    caseBarAccountService.accountDelete(params).then(function (response) {
      //刷新列表
      accountListSearch();
      //关闭弹窗
      closeBtn();
      //取消关闭弹窗
    });
  }
  //修改记账记录
  function accountEdit($index,id) {
    //获取案件的id
    vm.account = angular.copy(vm.filterBackParams[$index]);
    if (vm.account.moneyflow == 1) {
      //进入记账收入
      accountEditIncome(id);
    }else if(vm.account.moneyflow == 2){
      //进入记账支出
      accountEditExpend(id);
    }
  }

  //添加记账记录
  function accountAdd() {
    var params = {
      "caseId":vm.caseId,
      "content":vm.accountAddParams.content,
      "type":vm.showIcon,
      "amount":vm.accountAddParams.amount,
      "accountDate":$('.accountDate').val(),
      "remark":vm.accountAddParams.remark,
    };

    caseBarAccountService.accountAdd(params).then(function (response) {
      if(response.data.code == 1){
        //刷新页面
        accountListSearch();
        //关闭弹窗
        closeBtn();
      }else if(response.data.code ==0){


      }
    });
    vm.showIcon = '';
    vm.accountAddParams = { };
    vm.accountAddParams.accountDate=getNowFormatDate()
  }

  //保存
  function accountSave() {
    var params = {
      "id":vm.account.id,
      "content":vm.account.content,
      "type":vm.account.caseaccountid,
      "amount":vm.account.amount,
      "accountDate":vm.account.accountdate,
      "remark":vm.account.remark,
      "caseId":vm.caseId,
    };

    caseBarAccountService.accountAmend(params).then(function (response) {
      if(response.data.code == 1){
        //刷新页面
        accountListSearch();
        //关闭弹窗
        closeBtn();
      }
    })
  }
  //获取焦点
  function accountFocusIf(){
    $('input[name^="content"]').css('border', '1px solid #d9d9d9')
  }
  function accountFocusI(){
    $('input[name^="amount"]').css('border', '1px solid #d9d9d9')
  }
  //失去焦点保存校验
  function accountBlurIf(that){
    if(vm.accountAddParams.content==''){
      $('input[name^="content"]').css('border', '1px solid red')
  }
  }
  function accountBlurI(that){
    if(vm.accountAddParams.amount==''){
      $('input[name^="amount"]').css('border', '1px solid red')
    }
  }
  //保存校验
  function accountSubmitIf(){
    if(vm.accountAddParams.content=='') {
      $('input[name^="content"]').css('border', '1px solid red')
    }
    if(vm.accountAddParams.amount==''){
      $('input[name^="amount"]').css('border', '1px solid red')
    }
    if(typeof vm.showIcon =='object'){
        $('#globalErrorMessage').modal({
          backdrop: 'static',
          show: true,
          keyboard: false
        });
        $rootScope.errorMessage.show = true;
        $rootScope.errorMessage.content = '收支类型不能为空';

      /*var fatherBombBox=document.createElement('div');
      fatherBombBox.style.cssText='width:100%;height:520px;background:rgba(0,0,0,.4);position:absolute;left:0;top:0;';
      fatherBombBox.setAttribute('id','fatherBombBox');
      var BombBox=document.createElement('p');
      BombBox.style.cssText='width:300px;height:100px;position:absolute;left:50%;top:50%;margin-left:-150px;margin-top:-50px;border-radius:10px;font-size:20px;text-align:center;color:red;background:#fff;line-height:100px';
      BombBox.innerHTML='请选择分类';
      fatherBombBox.append(BombBox);
      $('.L_caseBarExpend').append(fatherBombBox);
      $('#fatherBombBox').click(function(){
        this.remove()
      })*/
    }
  }

  function caseAccountName(type) {
    if (!type) {
      return;
    }
    if (type < 10) {
      return vm.typeList.earningType[type];
    } else{
      return vm.typeList.expenseType[type];
    }
  }
  $('.unfold').click(function () {
    $('.unfold').css('color','##00BFB5')
  });


};
